#include "trace/fiu.hpp"

/**
 * \file
 * \ingroup trace
 * cas::TraceFiu implementation.
 */

namespace cas
{
    SLOG_COMPONENT_DEFINE("TraceFiu");

    TraceFiu::TraceFiu(std::string path) : TraceDriver(path)
    {
        SLOG_FUNCTION(path);
        data.clear();
    }

    TraceFiu::~TraceFiu()
    {
        SLOG_FUNCTION_NOARGS();
        data.clear();
    }

    size_t TraceFiu::dataSize()
    {
        return data.size();
    }

    bool TraceFiu::parseLine(std::istringstream &sin)
    {
        SLOG_FUNCTION_NOARGS();
        uint64_t timestp;
        char op;
        uint32_t inode;

        sin >> timestp >> op >> inode; // ignore other fields

        if (op == 'O' || op == 'C')
        {
            data.emplace_back(timestp, inode, true);
        }
        else if (op == 'S' || op == 'D')
        {
            data.emplace_back(timestp, inode, false);
        }

        return true;
    }

    void TraceFiu::repeat(uint32_t n)
    {
        if (n < 2 || n > 100)
        {
            return;
        }
        size_t l = data.size();
        data.resize(n * l);
        for (size_t i = 0; i < l; i++)
        {
            for (size_t j = 1; j < n; j++)
            {
                data[i + j * l] = data[i];
            }
        }
    }

} // namespace cas
